#include "../lvgl/lvgl.h"
#include <stdio.h>
#include <stdlib.h>
//在链表里面放下了文件路径和名字
typedef struct image_node
{
    char * file_path;
    struct image_node *next;
    struct image_node *prev;
}image_node;

static image_node *head_node = NULL;
static image_node *current_node = NULL;

//设置一个image文件判断函数bool类型
bool is_image_file(char* fliename)
{
   char *ext = lv_fs_get_ext(fliename);
   if (ext == NULL)
   {
    return false;
   }
   else if (strcmp(ext,"jpg") == 0 ||strcmp(ext,"png") == 0
          ||strcmp(ext,"gif") == 0 || strcmp(ext,"bmp") == 0)
    return true;
   
}
void scan_files(char *path)
{
    lv_fs_dir_t dir;
    if (lv_fs_dir_open(&dir,path) != LV_FS_RES_OK)
    {
        printf("无法打开目录\n");
    }
    char msg[256];//存储文件名字
    //定义一个尾节点指向空
    image_node *last_node = NULL;

    while (lv_fs_dir_read(&dir,msg) == LV_FS_RES_OK && strlen(msg)!=0)
    {
        //读取完目录后对文件扫描看是否为img文件
        if(is_image_file(msg) == true)
        {
            char pathAndname[512];
            sprintf(pathAndname,"%s%s",path,msg);
            //如果扫描到img文件，创造新节点，并且给新节点以及新节点的file_path分配空间
            image_node *new_node =(image_node*)malloc(sizeof(image_node));
            if (new_node == NULL)
            {
                perror("malloc error");
                continue;
            }
            new_node->file_path = (image_node*)malloc(strlen(pathAndname)+1);
            //把扫描到的节点名字和路径存到msg中，strcpy给新节点
            strcpy(new_node->file_path,pathAndname);
            new_node->next = NULL;

            if (head_node == NULL)
            {
                head_node = new_node;
                new_node->prev = NULL;
            }
            else
            {
                last_node->next = new_node;
                new_node->prev = last_node;
            }
            //更新最后一节车厢
            last_node = new_node;
            
        }
        
    }
    lv_fs_dir_close(&dir);
    //扫描完成了从第一张图片开始，也就是头节点
    current_node = head_node;
    
}

void update()
{
    if (current_node != NULL)
    {
        
    }
    
}

void imageview()
{



}